<template>
  <div class="app-container">
    <!--查询表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="24">
            <el-form-item label="账号名称">
              <el-input
                style="width: 100%"
                v-model="searchObj.keyword"
                placeholder="名称"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="fetchData()"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetData"
            >重置</el-button
          >
        </el-row>
      </el-form>
    </div>

    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="success" icon="el-icon-plus" size="mini" @click="add"
        >添 加</el-button
      >
    </div>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%; margin-top: 10px"
    >
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="communityName" label="小区名称" />
      <el-table-column prop="loginName" label="登录名称" />
      <el-table-column prop="telephone" label="手机" />
      <el-table-column prop="email" label="邮箱" />
      <el-table-column prop="roleType" label="账号类型"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit(scope.row.id)"
            title="修改"
          />
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="removeDataById(scope.row.id)"
            title="删除"
          />
          <el-button
            type="warning"
            icon="el-icon-baseball"
            size="mini"
            @click="showAssignRole(scope.row)"
            title="分配角色"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="分配角色" :visible.sync="dialogRoleVisible">
      <el-form label-width="80px">
        <el-form-item label="用户名">
          <el-input disabled :value="sysCount.loginName"></el-input>
        </el-form-item>

        <el-form-item label="角色列表">
          <el-checkbox
            :indeterminate="isIndeterminate"
            v-model="checkAll"
            @change="handleCheckAllChange"
            >全选</el-checkbox
          >
          <div style="margin: 15px 0"></div>
          <el-checkbox-group
            v-model="countRoleIds"
            @change="handleCheckedChange"
          >
            <el-checkbox
              v-for="role in allRoles"
              :key="role.id"
              :label="role.id"
              >{{ role.roleName }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="assignRole" size="small"
          >保存</el-button
        >
        <el-button @click="dialogRoleVisible = false" size="small"
          >取消</el-button
        >
      </div>
    </el-dialog>

    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
      <el-form
        ref="dataForm"
        :model="sysCount"
        label-width="150px"
        size="small"
        style="padding-right: 40px"
      >
        <el-form-item label="所属小区">
          <template>
            <el-select v-model="sysCount.communityId" placeholder="请选择">
              <el-option
                v-for="item in communityList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="登录名称">
          <el-input v-model="sysCount.loginName" />
        </el-form-item>
        <el-form-item label="登录密码">
          <el-input v-model="sysCount.password" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="sysCount.email" />
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="sysCount.telephone" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="dialogVisible = false"
          size="small"
          icon="el-icon-refresh-right"
          >取 消</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-check"
          @click="saveOrUpdate()"
          size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import api from "@/api/sysCount/count";
import comApi from "@/api/sysCommunity/community";

//引入角色api
import roleApi from "@/api/sysRole/role"
export default {
  data() {
    return {
      listLoading: true,
      list: [],
      total: 0,
      page: 1,
      limit: 8,
      searchObj: {},
      sysCount: {},
      dialogVisible: false,

      //小区列表
      communityList: [],

      //分配角色
      dialogRoleVisible: false,
      allRoles: [], // 所有角色列表
      countRoleIds: [], // 账号的角色ID的列表
      isIndeterminate: false, // 是否是不确定的
      checkAll: false, // 是否全选
    };
  },
  created() {
    this.fetchData(1);
  },
  methods: {
    //得到数据
    fetchData(page = 1) {
      this.page = page;
      api.getPageList(this.page, this.limit, this.searchObj).then((res) => {
        this.list = res.data.records;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },

    //重置按钮
    resetData() {
      // 清空表单
      this.searchObj = {};
      // 再次刷新列表数据
      this.fetchData();
    },
    //修改按钮
    edit(id) {
      api.getCountById(id).then((res) => {
        // console.log(res)
        this.sysCount = res.data;
        //弹出弹框
        this.openDialog();
      });
    },
    //添加按钮
    add() {
      this.sysCount = {};
      this.openDialog();
    },
    //开启弹框 查询选择框列表
    openDialog() {
      this.fetchCommunityList();
      this.dialogVisible = true;
    },

    //查询所有小区 放入选择框
    fetchCommunityList() {
      comApi.finAllCommunity().then((res) => {
        this.communityList = res.data;
      });
    },
    removeDataById(id) {
      this.$confirm("此操作将永久删除该账号, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // 点击确定
        api.removeById(id).then((response) => {
          // 1.给出提示
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.listLoading = true;
          //2. 再次刷新数据列表
          this.fetchData();
        });
      });
    },
    //保存/修改
    saveOrUpdate() {
      if (this.sysCount.id) {
        //修改
        this.update();
      } else {
        this.addCount();
      }
    },

    update() {
      api.updateCount(this.sysCount).then((res) => {
        // 1.给出提示
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //刷新列表
        this.fetchData();
        //关闭弹框
        this.dialogVisible = false;
      });
    },

    addCount() {
      api.addCount(this.sysCount).then((res) => {
        // 1.给出提示
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //刷新列表
        this.fetchData();
        //关闭弹框
        this.dialogVisible = false;
      });
    },

    //展示分配角色
    showAssignRole(row) {
      this.sysCount = row;
      

      roleApi.getRolesByUserId(row.id).then((response) => {
        this.dialogRoleVisible = true;
        console.log(response.data);
        this.allRoles = response.data.allRoles;
        this.countRoleIds = response.data.roleIds;
        this.checkAll = this.roleIds.length === this.allRoles.length;
        this.isIndeterminate =
          this.countRoleIds.length > 0 &&
          this.countRoleIds.length < this.allRoles.length;
      });
    },

    /*
    全选勾选状态发生改变的监听
    */
    handleCheckAllChange(value) {
      // value 当前勾选状态true/false
      // 如果当前全选, userRoleIds就是所有角色id的数组, 否则是空数组
      this.countRoleIds = value ? this.allRoles.map((item) => item.id) : [];
      // 如果当前不是全选也不全不选时, 指定为false
      this.isIndeterminate = false;
    },

    /*
    角色列表选中项发生改变的监听
    */
    handleCheckedChange(value) {
      const { countRoleIds, allRoles } = this;
      this.checkAll =
        countRoleIds.length === allRoles.length && allRoles.length > 0;
      this.isIndeterminate =
        countRoleIds.length > 0 && countRoleIds.length < allRoles.length;
    },

    //分配角色
    assignRole() {
      let assginVo = {
        countId: this.sysCount.id,
        roleIdList: this.countRoleIds,
      };
      roleApi.doAssign(assginVo).then((response) => {
        this.$message.success(response.message || "分配角色成功");
        this.dialogRoleVisible = false;
        this.fetchData(this.page);
      });
    },
  },
};
</script>

<style>
</style>